/**
 * vuex 对象操作事件
 * @author heke
 * @version 20170427
 */

import _ from 'lodash'
import _uuid from 'lodash-uuid'
import math from 'mathjs'

/**
 * 设置高亮项
 * @param commit
 * @param selector
 * @private
 * @returns {null}
 */
export const setSelector = ({ commit }, selector) => {
    commit('SET_SELECTOR', { selector })
}

/**
 * 重置高亮项
 * @param commit
 */
export const resetSelector = ({ commit }) => {
    commit('RESET_SELECTOR')
}

/**
 * 新建行
 * @param commit
 * @param row
 * @private
 */
export const initRow = ({ commit }) => {
    commit('INIT_ROW', {})
}

/**
 * 新建控件
 * @param dispatch
 * @param commit
 * @param getters
 * @param unitType
 */
export const initUnit = ({ dispatch, commit, getters }, unitType) => {
    try {
        // 校验
        if (!getters.isRowSelector && !getters.isUnitSelector) throw new Error('请选择行或控件')
        if (getters.isRowSelector && _.isEmpty(getters.selectedRow)) throw new Error(`该行不存在: ${getters.selector.rowGid}`)
        if (getters.isUnitSelector && _.isEmpty(getters.selectedUnit)) throw new Error(`该控件不存在: ${getters.selector.unitGid}`)

        const usableCols = math.subtract(12, _.sumBy(getters.selectedRow.units, 'col'))

        // 校验可用空间
        if (usableCols <= 0) throw new Error(`该行无法继续添加控件: ${getters.selector.rowGid}`)

        commit('INIT_UNIT', { selector: getters.selector, usableCols, unitType })
    } catch (ex) {
        dispatch('addAlertDefault', { text: ex.message, style: 'danger' })
    }
}

/**
 * 复制操作
 * @param dispatch
 * @param getters
 * @param commit
 */
export const copy = ({ dispatch, getters, commit }) => {
    try {
        if (!getters.isRowSelector && !getters.isUnitSelector) throw new Error('请选择行或控件')
        if (getters.isRowSelector && _.isEmpty(getters.selectedRow)) throw new Error(`该行不存在: ${getters.selector.rowGid}`)
        if (getters.isUnitSelector && _.isEmpty(getters.selectedUnit)) throw new Error(`该控件不存在: ${getters.selector.unitGid}`)

        if (getters.isUnitSelector) {
            commit('COPY', { type: getters.selectedUnit.unitType, context: getters.selectedUnit })
        }

        if (getters.isRowSelector) {
            commit('COPY', { type: 'row', context: getters.selectedRow })
        }
    } catch (ex) {
        dispatch('addAlertDefault', { text: ex.message, style: 'danger' })
    }
}

/**
 * 粘贴操作
 * @param dispatch
 * @param getters
 * @param commit
 */
export const paste = ({ dispatch, getters, commit }) => {
    try {
        if (_.isNil(getters.clipboard)) throw new Error('请先复制行或控件')

        // 剪贴板是行
        if (getters.clipboard.type === 'row') {
            // 深拷贝
            let row = _.cloneDeep(getters.clipboard.context)
            // 行id赋值
            row.rowGid = _uuid.uuid()
            // 所含控件的控件id赋值, 行id赋值一致
            row.units.forEach(item => {
                item.rowGid = row.rowGid
                item.unitGid = _uuid.uuid()
            })

            commit('INIT_ROW', { row })
        } else {
            const usableCols = math.subtract(12, _.sumBy(getters.selectedRow.units, 'col'))
            // 校验可用空间
            if (usableCols <= 0) throw new Error(`该行无法继续添加控件: ${getters.selector.rowGid}`)

            // 深拷贝
            let unit = _.cloneDeep(getters.clipboard.context)
            // 行id一致
            unit.rowGid = getters.selectedRow.rowGid
            // 控件id赋值
            unit.unitGid = _uuid.uuid()

            commit('INIT_UNIT', { selector: getters.selector, usableCols, unitType: getters.clipboard.type, unit })
        }
    } catch (ex) {
        dispatch('addAlertDefault', { text: ex.message, style: 'danger' })
    }
}

/**
 * 删除动作确认
 * @param dispatch
 * @param getters
 * @private
 */
export const remove = ({ dispatch, getters }) => {
    try {
        if (!getters.isRowSelector && !getters.isUnitSelector) throw new Error('请选择行或控件')
        if (getters.isRowSelector && _.isEmpty(getters.selectedRow)) throw new Error(`该行不存在: ${getters.selector.rowGid}`)
        if (getters.isUnitSelector && _.isEmpty(getters.selectedUnit)) throw new Error(`该控件不存在: ${getters.selector.unitGid}`)

        dispatch('addAlertConfirm', { text: '确认删除?', style: 'warning', callbacks: { ok: { callback: ['removeRowOrUnit'] } } })
    } catch (ex) {
        dispatch('addAlertDefault', { text: ex.message, style: 'danger' })
    }
}

/**
 * 删除动作
 * @param commit
 */
export const removeRowOrUnit = ({ commit }) => {
    commit('REMOVE_ROW_OR_UNIT')
    // 重置高亮项
    commit('RESET_SELECTOR')
}

/**
 * 获取控件object
 * @param dispatch
 * @param state
 * @param rowGid
 * @param unitGid
 * @private
 */
export const getUnit = ({ dispatch, state }, { rowGid, unitGid }) => {
    try {
        let row = _.find(state.formData.rows, { rowGid: rowGid })
        if (_.isEmpty(row)) {
            throw new Error(`该行不存在: ${rowGid}`)
        }
        let unit = _.find(row.units, { unitGid: unitGid })
        if (_.isEmpty(unit)) {
            throw new Error(`该控件不存在: ${unitGid}`)
        }
        return unit
    } catch (ex) {
        dispatch('addAlertDefault', { text: ex.message, style: 'danger' })
    }
}

/**
 * 设置控件
 * @param dispatch
 * @param commit
 * @param unit
 * @private
 */
export const setUnit = ({ dispatch, commit }, unit) => {
    try {
        commit('SET_UNIT', { unit })
        dispatch('reload')
    } catch (ex) {
        dispatch('addAlertDefault', { text: ex.message, style: 'danger' })
    }
}

export const resetUnit = ({ dispatch, commit }, unit) => {
    try {
        debugger
        const DynamicClass = Object.getPrototypeOf(unit)
        const dynamicInstance = new DynamicClass.constructor({ rowGid: unit.rowGid, unitGid: unit.unitGid })
        commit('SET_UNIT', { unit: dynamicInstance })
    } catch (ex) {
        dispatch('addAlertDefault', { text: ex.message, style: 'danger' })
    }
}

/**
 * 重载workspace
 * @param commit
 * @param delay
 */
export const reload = ({ commit }, delay = 100) => {
    commit('RELOAD_ON')
    _.delay(() => {
        commit('RELOAD_OFF')
    }, delay)
}
